package com.newsanalysis.controller;

import com.newsanalysis.model.entity.News;
import com.newsanalysis.service.NewsService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/news")
@RequiredArgsConstructor
public class NewsController {
    
    private final NewsService newsService;
    
    /**
     * 显示新闻列表
     */
    @GetMapping
    public String newsList(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "15") int size,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String keyword,
            Model model) {
        
        Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "publishTime"));
        Page<News> newsPage;
        
        if (category != null && !category.isEmpty()) {
            newsPage = newsService.findByCategory(category, pageable);
            model.addAttribute("category", category);
        } else if (keyword != null && !keyword.isEmpty()) {
            newsPage = newsService.searchByKeyword(keyword, pageable);
            model.addAttribute("keyword", keyword);
        } else {
            newsPage = newsService.findAll(pageable);
        }
        
        model.addAttribute("newsPage", newsPage);
        return "news-list";
    }
    
    /**
     * 显示新闻详情
     */
    @GetMapping("/{id}")
    public String newsDetail(@PathVariable Long id, Model model) {
        return newsService.findById(id)
                .map(news -> {
                    model.addAttribute("news", news);
                    return "news-detail";
                })
                .orElse("redirect:/news?error=notfound");
    }
    
    /**
     * 按分类浏览新闻
     */
    @GetMapping("/category/{category}")
    public String newsByCategory(
            @PathVariable String category,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "15") int size,
            Model model) {
        
        Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "publishTime"));
        Page<News> newsPage = newsService.findByCategory(category, pageable);
        
        model.addAttribute("category", category);
        model.addAttribute("newsPage", newsPage);
        return "news-list";
    }
    
    /**
     * 搜索新闻
     */
    @GetMapping("/search")
    public String searchNews(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "15") int size,
            Model model) {
        
        if (keyword == null || keyword.trim().isEmpty()) {
            return "redirect:/news";
        }
        
        Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "publishTime"));
        Page<News> newsPage = newsService.searchByKeyword(keyword.trim(), pageable);
        
        model.addAttribute("keyword", keyword);
        model.addAttribute("newsPage", newsPage);
        return "news-list";
    }
}
